﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;

namespace BeatEmUp.Menu
{
    public class MenuMainComponent
    {
        public MenuOptionsScreen optionsScreen;
        public MenuScoresScreen scoresScreen;
        public MenuMainScreen mainScreen;
        public MenuExitScreen exitScreen;
        public MenuIntroScreen introScreen;
        GameManager game;
        KeyboardState oldState;
        public bool gameWasTurnedOn { get; set; }
        public bool MenuEnabled { get; set; }
        public List<int> scoreList;

        public MenuMainComponent(GameManager game, bool Enabled)
        {
                MenuEnabled = Enabled;
                scoreList = new List<int>();
                this.game = game;
                optionsScreen = new MenuOptionsScreen(game);
                scoresScreen = new MenuScoresScreen(game);
                mainScreen = new MenuMainScreen(game);
                exitScreen = new MenuExitScreen(game);
                introScreen = new MenuIntroScreen(game);
                gameWasTurnedOn = false;
                Initialize();        
        }

        public void StartNewGame()
        {
            // There should be a method here that starts a new game.
            this.game.LoadObjects();
            gameWasTurnedOn = true;
            SoundManager.StopMusic();
            SoundManager.PlayGameMusic();
        }

        public void Initialize()
        {
            if (MenuEnabled)
            {
                optionsScreen.isVisibleAndActive = false;
                scoresScreen.isVisibleAndActive = false;
                mainScreen.isVisibleAndActive = false;
                exitScreen.isVisibleAndActive = false;
                introScreen.isVisibleAndActive = true;
            }
        }

        public void LoadContent(ContentManager content)
        {
            if (MenuEnabled)
            {
                introScreen.LoadContent(content);
                mainScreen.LoadContent(content);
                optionsScreen.LoadContent(content);
                scoresScreen.LoadContent(content);
                exitScreen.LoadContent(content);
            }
        }

        public void Update(GameTime gameTime)
        {
            KeyboardState newState = Keyboard.GetState();
            MouseState newMouseState = Mouse.GetState();

            if (MenuEnabled == true)
            {
                if (introScreen.isVisibleAndActive == true) introScreen.Update(gameTime, this);
                if (mainScreen.isVisibleAndActive == true) mainScreen.Update(gameTime, this);
                if (optionsScreen.isVisibleAndActive == true) optionsScreen.Update(gameTime, this);
                if (scoresScreen.isVisibleAndActive == true) scoresScreen.Update(gameTime, this);
                if (exitScreen.isVisibleAndActive == true) exitScreen.Update(gameTime, this);

                if ((newState.IsKeyDown(Keys.Escape)) && (!oldState.IsKeyDown(Keys.Escape)))
                {
                    if (gameWasTurnedOn == true)
                    {
                        mainScreen.isVisibleAndActive = !mainScreen.isVisibleAndActive;

                        for (int i = 0; i < 100000000; i++) ;
                    }
                }
                oldState = newState;
            }
        }

        public bool areSomeMenuComponentsActive()
        {
                if (introScreen.isVisibleAndActive == false &&
                mainScreen.isVisibleAndActive == false &&
                optionsScreen.isVisibleAndActive == false &&
                scoresScreen.isVisibleAndActive == false &&
                exitScreen.isVisibleAndActive == false)
                    return false;
                else
                {
                    return true;
                }
        }

        public void Draw(SpriteBatch sb)
        {
            if (MenuEnabled)
            {
                if (introScreen.isVisibleAndActive == false &&
                    mainScreen.isVisibleAndActive == false &&
                    optionsScreen.isVisibleAndActive == false &&
                    scoresScreen.isVisibleAndActive == false &&
                    exitScreen.isVisibleAndActive == false)
                {
                    ((GameManager)this.game).IsMouseVisible = false;
                }
                else
                {
                    ((GameManager)this.game).IsMouseVisible = true;
                }

                if (introScreen.isVisibleAndActive == true) introScreen.Draw(sb);
                if (mainScreen.isVisibleAndActive == true) mainScreen.Draw(sb, this);
                if (optionsScreen.isVisibleAndActive == true) optionsScreen.Draw(sb);
                if (scoresScreen.isVisibleAndActive == true) scoresScreen.Draw(sb, scoreList);
                if (exitScreen.isVisibleAndActive == true) exitScreen.Draw(sb);
            }
        }

        public MenuOptionsScreen GetOptionsScreen() { return optionsScreen; }
        public MenuScoresScreen GetScoresScreen() { return scoresScreen; }
        public MenuMainScreen GetMainScreen() { return mainScreen; }
        public MenuExitScreen GetExitScreen() { return exitScreen; }
        public MenuIntroScreen GetIntroScreen() { return introScreen; }


    }
}
